package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.LabelProviderCollaborator;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.Home;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.MessageBox;

public class Ccollaborators {

	Home view;
	private Collaborator collaboratorActif;
	private boolean admin;
	
	public Ccollaborators(Home view) {
		this.view = view;
	}

	public void init() {

		// Mise des éléments en invisible et désactivé
		view.getGrpCollaborators().setVisible(false);
		view.getTltmEditCollaborator().setEnabled(false);
		view.getTltmDelCollaborator().setEnabled(false);

		view.getTableViewerCollaborators().setContentProvider(new ArrayContentProvider());
		view.getTableViewerCollaborators().setLabelProvider(new LabelProviderCollaborator());
		view.getTableViewerCollaborators().setInput(DAO.getCollaborators());

	}

	public void listener() {

		// getTableCollaborators --> Table collaborators
		view.getTableCollaborators().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerCollaborators().getSelection();
				setCollaboratorActif((Collaborator) sel.getFirstElement());

				view.getTltmEditCollaborator().setEnabled(true);
				view.getTltmDelCollaborator().setEnabled(true);
				view.getTltmEditCollaborator().setEnabled(true);

				view.getLblViewCollaborator().setText("View collaborator");
				view.getGrpCollaborators().setVisible(true);

				view.getTxtFirstnameCollaborators().setEditable(false);
				view.getTxtLastNameCollaborators().setEditable(false);
				view.getTxtEmailCollaborators().setEditable(false);
				view.getTxtPasswordCollaborators().setEditable(false);
				view.getTxtConfirmPasswordCollaborators().setEditable(false);
				view.getBtnCollaborators().setVisible(false);
				view.getCheckButtonCollaborator().setEnabled(false);

				if(collaboratorActif.getAdministrator()==false){
					admin=false;
				}else{
					admin=true;
				}
				
				view.getTxtFirstnameCollaborators().setText(collaboratorActif.getFirstname());
				view.getTxtLastNameCollaborators().setText(collaboratorActif.getLastname());
				view.getTxtEmailCollaborators().setText(collaboratorActif.getEmail());
				view.getTxtPasswordCollaborators().setText(collaboratorActif.getPassword());
				view.getCheckButtonCollaborator().setSelection(admin);;
				view.getTxtConfirmPasswordCollaborators().setText("");

			}
		});

		// getTltmAddCollaborator --> Add
		view.getTltmAddCollaborator().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				// Mettre les bouton editer et supprimer non clicable
				view.getTltmEditCollaborator().setEnabled(false);
				view.getTltmDelCollaborator().setEnabled(false);

				// getGrpCollaborators
				view.getLblViewCollaborator().setText("Add collaborator");
				view.getGrpCollaborators().setVisible(true);

				// getBtnCollaborators
				view.getBtnCollaborators().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnCollaborators().setText("Add this collaborator");

				// Mettre les txtBox vide
				view.getTxtFirstnameCollaborators().setText("");
				view.getTxtLastNameCollaborators().setText("");
				view.getTxtEmailCollaborators().setText("");
				view.getTxtPasswordCollaborators().setText("");
				view.getTxtConfirmPasswordCollaborators().setText("");
				view.getCheckButtonCollaborator().setSelection(false);

				view.getTxtFirstnameCollaborators().setEditable(true);
				view.getTxtLastNameCollaborators().setEditable(true);
				view.getTxtEmailCollaborators().setEditable(true);
				view.getTxtPasswordCollaborators().setEditable(true);
				view.getTxtConfirmPasswordCollaborators().setEditable(true);
				view.getBtnCollaborators().setVisible(true);
				view.getCheckButtonCollaborator().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// getTltmEditCollaborator --> Edit
		view.getTltmEditCollaborator().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				// getGrpCollaborators
				view.getLblViewCollaborator().setText("Edit collaborator");
				view.getGrpCollaborators().setVisible(true);

				// getBtnCollaborators
				view.getBtnCollaborators().setImage(Utils.getImage(Utils.IMG_EDIT));
				view.getBtnCollaborators().setText("Save this collaborator");

				// Ajout des éléments dans les txtBox
				
				if(collaboratorActif.getAdministrator()==false){
					admin=false;
				}else{
					admin=true;
				}
				
				view.getTxtFirstnameCollaborators().setText(collaboratorActif.getFirstname());
				view.getTxtLastNameCollaborators().setText(collaboratorActif.getLastname());
				view.getTxtEmailCollaborators().setText(collaboratorActif.getEmail());
				view.getTxtPasswordCollaborators().setText(collaboratorActif.getPassword());
				view.getTxtConfirmPasswordCollaborators().setText("");
				view.getCheckButtonCollaborator().setSelection(admin);

				view.getTxtFirstnameCollaborators().setEditable(true);
				view.getTxtLastNameCollaborators().setEditable(true);
				view.getTxtEmailCollaborators().setEditable(true);
				view.getTxtPasswordCollaborators().setEditable(true);
				view.getTxtConfirmPasswordCollaborators().setEditable(true);
				view.getBtnCollaborators().setVisible(true);
				view.getCheckButtonCollaborator().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getBtnCollaborators().getText() == "Add this collaborator") {
					if (view.getTxtFirstnameCollaborators().getText() == "" || view.getTxtLastNameCollaborators().getText() == "" || view.getTxtEmailCollaborators().getText() == "" || view.getTxtPasswordCollaborators().getText() == "" || view.getTxtConfirmPasswordCollaborators().getText() == "") {
						Utils.MsgBox(view.getShell(), "Please, complete all fiels", "information");
					} else {
						boolean nomUtilise = false;
						for (Collaborator collaborator : DAO.getCollaborators()) {
							if (collaborator.getEmail().equals(view.getTxtEmailCollaborators().getText())) {
								nomUtilise = true;
							}
						}
						if (nomUtilise == true) {
							Utils.MsgBox(view.getShell(), "A collaborator already has this email", "information");
						} else {
							if (!view.getTxtPasswordCollaborators().getText().equals(view.getTxtConfirmPasswordCollaborators().getText())) {
								Utils.MsgBox(view.getShell(), "Passwords are not the same", "information");
							} else {
								if(view.getCheckButtonCollaborator().getSelection()){
									admin=true;
								}else{
									admin=false;
								}
								Collaborator collaborator = new Collaborator(view.getTxtFirstnameCollaborators().getText(), view.getTxtPasswordCollaborators().getText(), view.getTxtFirstnameCollaborators().getText(), view.getTxtLastNameCollaborators().getText(), view.getTxtEmailCollaborators().getText(), admin, null, null, null, null);
								DAO.SQLCollaborator(collaborator, "insert");
								view.getTableViewerCollaborators().add(collaborator);
								Utils.MsgBox(view.getShell(), "Collaborator is create", "information");
								init();

							}
						}
					}
				} else {
					if (view.getTxtFirstnameCollaborators().getText() == "" || view.getTxtLastNameCollaborators().getText() == "" || view.getTxtEmailCollaborators().getText() == "" || view.getTxtPasswordCollaborators().getText() == "") {
						Utils.MsgBox(view.getShell(), "Please, complete all fiels", "information");
					} else {
						Boolean email = false;
						java.util.List<Collaborator> verifEmail = DAO.getCollaborators();
						for (Collaborator emailV : verifEmail) {
							if (view.getTxtEmailCollaborators().getText().equals(emailV.getEmail())) {
								if (emailV.getEmail().equals(collaboratorActif.getEmail())) {
									email = false;
								} else {
									email = true;
								}
							}
						}

						if (email == true) {
							Utils.MsgBox(view.getShell(), "Email already exist", "information");
						} else {
							if (!view.getTxtPasswordCollaborators().getText().equals(view.getTxtConfirmPasswordCollaborators().getText())) {
								if (view.getTxtConfirmPasswordCollaborators().getText() == "") {
									if (view.getTxtPasswordCollaborators().getText().equals(collaboratorActif.getPassword())) {
										if(view.getCheckButtonCollaborator().getSelection()){
											admin=true;
										}else{
											admin=false;
										}
										collaboratorActif.setAdministrator(admin);
										collaboratorActif.setFirstname(view.getTxtFirstnameCollaborators().getText());
										collaboratorActif.setLastname(view.getTxtLastNameCollaborators().getText());
										collaboratorActif.setEmail(view.getTxtEmailCollaborators().getText());
										collaboratorActif.setPassword(view.getTxtPasswordCollaborators().getText());
										DAO.SQLCollaborator(collaboratorActif, "update");
										view.getTableViewerCollaborators().refresh();
										Utils.MsgBox(view.getShell(), "Collaborator is update", "information");
									} else {
										Utils.MsgBox(view.getShell(), "This is not your current password. \nPlease confirm your password.", "information");
									}
								} else {
									Utils.MsgBox(view.getShell(), "Passwords are not the same", "information");
								}
							} else {
								if(view.getCheckButtonCollaborator().getSelection()){
									admin=true;
								}else{
									admin=false;
								}
								collaboratorActif.setAdministrator(admin);
								collaboratorActif.setFirstname(view.getTxtFirstnameCollaborators().getText());
								collaboratorActif.setLastname(view.getTxtLastNameCollaborators().getText());
								collaboratorActif.setEmail(view.getTxtEmailCollaborators().getText());
								collaboratorActif.setPassword(view.getTxtPasswordCollaborators().getText());
								DAO.SQLCollaborator(collaboratorActif, "update");
								view.getTableViewerCollaborators().refresh();
								Utils.MsgBox(view.getShell(), "Collaborator is update", "information");
							}
						}
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmDelCollaborator().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				MessageBox messageBoxCollaborator = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBoxCollaborator.setText("SCRUMTOOL");
				messageBoxCollaborator.setMessage("Do you really want to delete this collaborator ?");
				int responseCollaborator = messageBoxCollaborator.open();
				if (responseCollaborator == SWT.YES) {
					DAO.SQLCollaborator(collaboratorActif, "delete");
					view.getTableViewerCollaborators().remove(collaboratorActif);
					view.getGrpCollaborators().setVisible(false);
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void setCollaboratorActif(Collaborator collaboratorActif) {
		this.collaboratorActif = collaboratorActif;
	}

}
